<?php
/**
 * User: 黄城
 * Date: 2017/4/17
 * Time: 11:32
 * Description: 商品分类销售统计
 */

namespace Admin\Controller;

use Think\Model;
use Think\Page;

class StoreItemSellController extends BaseController
{
    public $map;

    public function __construct()
    {
        parent::__construct();
        $this->map = ['local_id' => session('userInfo')['local_id']];
    }

    public function index()
    {
        $view = I('get.view', 1);
        $this->assign('view', $view);
        switch ($view){
            case 1:
                $this->sellCat();
                $this->display('StoreCat/sellCat');
                break;
            case 2:
                $this->ranking();
                $this->display('StoreCat/ranking');
                break;
            default:
                $this->redirect('StoreItemSell/index');
        }
        exit(200);
    }

    /**
     * 商品销售分类
     */
    public function sellCat()
    {
        $page = I('get.p', 1);
        $category = I('get.category', '', 'htmlspecialchars');
        $beginDate = I('get.begin_time', '', 'htmlspecialchars');
        $endDate = I('get.end_time', '', 'htmlspecialchars');
        $parameter = [
            'p'=>$page,
            'view'=>1
        ];

        if (!empty($beginDate) and !empty($endDate) and strtotime($beginDate) <= strtotime($endDate)){
            $bTime = strtotime($beginDate);
            $eTime = strtotime($endDate)+86400;
            $this->map['UNIX_TIMESTAMP(time)'] = [['egt', $bTime], ['lt', $eTime]];
            $parameter['begin_time'] = $beginDate;
            $parameter['end_time'] = $endDate;
        }else{
            $beginDate = '';
            $endDate = '';
        }

        $categorys = explode(',', $category);
        if (!empty($category)){
            $parameter['category'] = $category;
            $this->map['goods_cate_id'] = ['in', $category];
        }

        //查询数据
        $lists = M('statistical_goods_cate')
            ->where($this->map)->page($page.",15")
            ->order('`time` DESC')
            ->field(['FROM_UNIXTIME(UNIX_TIMESTAMP(`time`), \'%Y-%m-%d\') `time`', 'goods_cate_id', 'name', 'money', 'num'])->select();

        //获取分页
        $count = M('statistical_goods_cate')->where($this->map)->field(['id'])->count();

        $PageObject = new Page($count, 15,$parameter);
        $show = $PageObject->show();

        $this->assign('data', ['beginDate' => $beginDate, 'endDate' => $endDate, 'page'=>$show]);
        $this->assign('category', $this->getCategory());
        $this->assign('selects', $categorys);
        $this->assign('lists', $lists);

    }

    /**
     * 商品分类
     */
    private function getCategory()
    {
        $lists = M('b2c_store_cat')->where(['local_id' => session('userInfo')['local_id']])->order('parent_id')
            ->field(['store_cat_id cat_id', 'parent_id', 'cat_name'])->select();

        $category = array();
        foreach ($lists as $value){
            if ($value['parent_id'] == 0){
                $category[$value['cat_id']]['name'] = $value['cat_name'];
                $category[$value['cat_id']]['sub_cat'] = array();
                continue;
            }
            $category[$value['parent_id']]['sub_cat'][$value['cat_id']] = $value['cat_name'];
        }
        return $category;
    }

    /**
     * 商品销售排行Top100
     */
    public function ranking()
    {
        $page = I('get.p', 1);
        $sort = I('get.sort', 'goods_num', 'htmlspecialchars');
        $parameter = [
            'p'=>$page,
            'view'=>2,
            'sort'=>$sort
        ];

        //获取不执行的SQL
        $querySql = M('statistical_sales_goods')
            ->where($this->map)->group('goods_id')->order("sum(`$sort`) desc")
            ->field(['goods_name p_name', 'SUM(`goods_num`) nums', 'SUM(`money`) amount, barcode'])->limit(100)->select(false);

        //查询数据
        $model = new Model();
        $lists = $model->table($querySql.' a')
            ->page($page.',20')->select();
        $this->assign('lists', $lists);
        //分页显示
        $count = $model->table($querySql.' d')->count();
        $PageObject = new Page($count, 20,$parameter);
        $show = $PageObject->show();
        $this->assign('page', $show);
        $this->assign('sort', $sort);
    }
}